import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
const service = axios.create({
  baseURL: '/api',
  timeout: 5000
})
service.interceptors.request.use(
  config => {
    if (store.getters.getToken) {
      config.headers['Authorization'] = store.getters.getToken
    }
    return config
  },
  error => {
    Message.warning('系统异常，请及时联系管理')
    return Promise.reject('error')
  }
)

service.interceptors.response.use(
  response => {
    const res = response.data
    if (res.code == 200) {
      return res
    } else if(res.code == 401){
      this.$router.push('/login')
      return Promise.reject('error')
    }
    else{
      Message.error(res.message || '操作失败！')
      return Promise.reject('error')
    } 
  },
  error => {
    Message.error('请求失败或登录超时')
  }
)

export default service
